<?php
// PHP 4.1
global $wpdb,$General;

// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';

foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}

// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
//$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);

if (!$fp) {
// HTTP ERROR
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = fgets ($fp, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
$fromEmail = $General->get_site_emailId();
		$fromEmailName = $General->get_site_emailName();
		$paymentOpts = $General->get_payment_optins('paypal');
		$merchantid = $paymentOpts['merchantid'];
		
		$item_name            = $_POST['item_name'];
		$item_number          = $_POST['item_number'];
		$quantity             = $_POST['quantity'];
		$payment_amount       = $_POST['mc_gross'];
		$fee                  = $_POST['mc_fee'];
		$tax                  = $_POST['tax'];
		$payment_currency     = $_POST['mc_currency'];
		$exchange_rate        = $_POST['exchange_rate'];
		$payment_status       = $_POST['payment_status'];
		$payment_type         = $_POST['payment_type'];
		$payment_date         = $_POST['payment_date'];
		$txn_id               = $_POST['txn_id'];
		$txn_type             = $_POST['txn_type']; // 'cart', 'send_money' or 'web_accept' (manual page 46)
		$custom               = $_POST['custom'];   // Any custom data
		$receiver_email       = $_POST['receiver_email'];
		$first_name           = $_POST['first_name'];
		$last_name            = $_POST['last_name'];
		$payer_business_name  = $_POST['payer_business_name'];
		$payer_email          = $_POST['payer_email'];
		$address_street       = $_POST['address_street'];
		$address_zip          = $_POST['address_zip'];
		$address_city         = $_POST['address_city'];
		$address_state        = $_POST['address_state'];
		$address_country      = $_POST['address_country'];
		$address_country_code = $_POST['address_country_code'];
		$residence_country    = $_POST['residence_country'];
		$General->set_ordert_status($custom,'approve');
		
		///////////email start//////////
		$store_name = get_option('blogname');
		$message1  = "";
		$message1 .= "<p>--------------------------------------------------</p>";
		$message1 .= "<p>Payment Details for Order ID #$custom</p>";
		$message1 .= "<p>--------------------------------------------------</p>";
		$message1 .= " <p>Order Information: <br/><br/> $item_name </p>";
		$message1 .= "<p>--------------------------------------------------</p>";
		$message1 .= "<p>Trans ID: $txn_id</p>";
		$message1 .= " <p> Status: $payment_status</p>";
		$message1 .= " <p>  Type: $payment_type</p>";
		$message1 .= " <p> Date: $payment_date</p>";
		$message1 .= " <p> Method: $txn_type</p>";
		$message1 .= " <p> Receiver Email: $receiver_email</p>";
		$message1 .= " <p> Payer Email: $payer_email</p>";
		$message1 .= " <p> Payment Currency: $payment_currency</p>";
		$message1 .= " <p> Payment Date: $payment_date</p>";
		$message1 .= "<p>--------------------------------------------------</p>";
		
		$supplier_subject = get_option('order_success_ipn_supplier_email_subject');
		$supplier_message = get_option('order_success_ipn_supplier_email_content');
		$search_array = array('[#$user_name#]','[#$transaction_details#]','[#$store_name#]');
		$replace_array = array($fromEmailName,$message1,'Pay Pal');
		$supplier_message = str_replace($search_array,$replace_array,$supplier_message);
		
		if(CHILDTEMPLATEPATH && file_exists(CHILDTEMPLATEPATH . '/ipn_process_before_email_customer.php'))
		{
			include_once(CHILDTEMPLATEPATH . '/ipn_process_before_email_customer.php');
		}
		//////////////////email end/////////
		if(get_option('order_success_ipn_supplier_email_flag')!='inactive')
		{
			$General->sendEmail($fromEmail,$fromEmailName,$payer_email,$first_name,$supplier_subject,$supplier_message,$extra='');///To admin email
		}
		if(CHILDTEMPLATEPATH && file_exists(CHILDTEMPLATEPATH . '/ipn_process_before_email_admin.php'))
		{
			include_once(CHILDTEMPLATEPATH . '/ipn_process_before_email_admin.php');
		}
		
		$customer_subject = get_option('order_success_ipn_client_email_subject');
		$customer_message = get_option('order_success_ipn_client_email_content');
		$search_array = array('[#$user_name#]','[#$transaction_details#]','[#$store_name#]');
		$replace_array = array($fromEmailName,$message1,'Pay Pal');
		$customer_message = str_replace($search_array,$replace_array,$customer_message);
		if(get_option('order_success_ipn_client_email_flag')!='inactive')
		{
			$General->sendEmail($payer_email,$first_name,$fromEmail,$fromEmailName,$customer_subject,$customer_message,$extra='');///To client email
		}
}
else if (strcmp ($res, "INVALID") == 0) {
// log for manual investigation
}
}
fclose ($fp);
}
?>